package com.firstgroup.repository;

import com.firstgroup.domain.Comment;
import com.firstgroup.domain.Interviewers;
import com.firstgroup.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import javax.transaction.Transactional;
import java.util.List;

/**
 * 创作人 王箫凯
 * Date 2021/8/9
 * Time 8:00
 *
 * @author 王箫凯
 */
public interface UserRepository extends JpaRepository<User, Integer>, JpaSpecificationExecutor<User> {
    @Query(value = "select u.id,u.name from User u")
    List<Object> findIdAndName();


     User findUserByUsername(String username);
     User findByUsername(String username);

    /**
     * 更新面试人的状态
     *
     * @param interviewId ID
     * @param state     状态
     * @return
     */
    @Query(value = "update interviewers set state = ?2 where interviewers_id=?1  order by regist_time desc limit 1 ",nativeQuery = true)
    @Modifying
    int updateState(@Param("interviewId") Integer interviewId, @Param("state") String state);

    /**
     * 根据id进行修改状态
     * @param id id
     * @param state 修改后的状态
     * @return 返回数值，进行一次 就是1
     */
    @Query("update Interviewers set state = ?2 where interviewersId = ?1")
    @Modifying
    int updateStateByInterviewId(Integer id,String state);
    /**
     * 根据ID查询用户
     * @param id 用户ID
     * @return 返回一个user独享
     */
    User findById(int id);


    @Transactional
    @Query("update User set name = ?2,username=?3 where id=?1")
    @Modifying
    int updateUser(int id,String name,String username);

    /**
     * 查询黑名单
     * @return 返回一个List集合
     */
    @Query(value = "select q from Comment  q where q.state ='黑名单'")
    List<Comment>  findAllBanUser();

    /**
     * 根据用户ID进行判断是否已经被拉入黑名单
     * @param interviewId 用户ID
     * @return 返回一个List 集合
     */
    @Query(value = "select q from Comment  q where (q.interviewrId=?1 and q.state ='黑名单')")
    Comment findBanUserByInterViewId(int interviewId);





}
